package com.vf.admin.client.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppMounts;
import com.vf.admin.miaodong.entity.AppUserDressup;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 坐骑
 */
@Repository
public class AppMountsDao extends BaseDao<AppMounts> {


    /**
     * 查询列表
     */
    public List<AppMounts> shopList(SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_mounts where 1=1 ");
        sbStr.append(" and state = 1  ");
        sbStr.append(" and guishu=2 ");
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppMounts.class,splitPage);
    }

    /**
     * 详情
     */
    public AppMounts get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_mounts where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppMounts.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppMounts haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }


    /**
     * 后天管理获取坐骑
     * @param map
     * @param bean
     * @return
     */
    public List<AppMounts> findList(Map<String, Object> map, SplitPageBean bean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT a.* FROM app_mounts a where 1=1 ");
        if (StringEx.isNotNull(map.get("name"))) {
            sbStr.append(" and a.name like ? ");
            paramList.add("%" + map.get("name") + "%");
        }
        sbStr.append(" order by a.createtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppMounts.class, bean);
    }

    public List<AppUserDressup> selectAll() {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * FROM app_user_dressup as um GROUP BY um.id HAVING um.type != 3 && ((um.`day`*24*60*60)-SUM(timestampdiff(SECOND,um.buyingtime,now()))) <0");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppUserDressup.class);
    }

    public Map selectMounts(Integer userid) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("SELECT am.imgurl,am.`name`,am.effects,d.userid FROM app_user_dressup as d left join app_mounts as am on am.id = d.mountsid where d.userid = ? and d.type = 1 and d.state = 1");
        paramList.add(userid);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, Map.class);
    }
}
